package com.shine.chemicalWholeProcessSystem.ui.riskManagement;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.shine.chemicalWholeProcessSystem.BR;
import com.shine.chemicalWholeProcessSystem.R;
import com.shine.chemicalWholeProcessSystem.app.Constants;
import com.shine.chemicalWholeProcessSystem.base.BaseActivity;
import com.shine.chemicalWholeProcessSystem.bean.RiskManagementBean;
import com.shine.chemicalWholeProcessSystem.databinding.ActivityRiskManagementCreateBinding;
import com.shine.chemicalWholeProcessSystem.databinding.ActivityRiskManagementDetailsBinding;
import com.shine.chemicalWholeProcessSystem.http.ApiException;
import com.shine.chemicalWholeProcessSystem.http.BaseReturn;
import com.shine.chemicalWholeProcessSystem.http.ECHttpResponse;
import com.shine.chemicalWholeProcessSystem.http.ECObserver;
import com.shine.chemicalWholeProcessSystem.http.RetrofitClient;
import com.shine.chemicalWholeProcessSystem.http.RxUtils;
import com.shine.chemicalWholeProcessSystem.http.Service;

import java.util.ArrayList;
import java.util.List;

public class RiskManagementDetailsActivity extends BaseActivity<ActivityRiskManagementDetailsBinding, RiskManagementDetailsViewModel> {
    private final int REFRESH = 1000;
    private String id;
    private RiskManagementBean riskManagementBean;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_risk_management_details;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RiskManagementDetailsViewModel initViewModel() {
        return new RiskManagementDetailsViewModel(this);
    }

    @Override
    public int appTheme() {
        return Constants.THEME_FULL;
    }

    @Override
    public void initData() {
        super.initData();
        setStatusBar();
        initTitle(this, "风险点管理");

        showView((RiskManagementBean) getIntent().getSerializableExtra(Constants.BUNDLE_EXTRA));

        binding.delete.setOnClickListener(this);
        binding.edit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.delete:
                new XPopup.Builder(RiskManagementDetailsActivity.this)
                        .isDestroyOnDismiss(true)
                        .asConfirm("提示", "确认要删除这条数据", new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                delDangerousPoint(id);
                            }
                        })
                        .show();
                break;
            case R.id.edit:
                Bundle bundle = new Bundle();
                bundle.putBoolean(Constants.BUNDLE_EDIT, true);
                bundle.putSerializable(Constants.BUNDLE_EXTRA, riskManagementBean);
                switchActivityForResult(RiskManagementCreateActivity.class, REFRESH, bundle);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REFRESH:
                    finishActivityForResult();
                    break;
                default:
                    break;
            }
        }
    }

    private void showView(RiskManagementBean bean) {
        riskManagementBean = bean;
        id = bean.getId();
        binding.companyName.setText(bean.getLeader());
        binding.phone.setText(bean.getContactnum());
        binding.riskName.setText(bean.getPointname());
        binding.number.setText(bean.getPointcode());
//        binding.riskClassification.setText(bean.getRiskclass());
//        binding.isMajorHazard.setText(bean.getIsMajorHazard());
//        binding.riskClassificationMethod.setText(bean.getClassmethod());
        binding.possibility.setText(bean.getPossibility());
//        binding.exposureFrequency.setText(bean.getExposureFrequency());
//        binding.severity.setText(bean.getSeverity());
//        binding.riskLevel.setText(bean.getRiskLevel());
        binding.siteName.setText(bean.getSitename());
        binding.industry.setText(bean.getIndustry());
//        binding.industryCategory.setText(bean.getIndustrycategory());
        binding.position.setText(bean.getPosition());
//        binding.accidentProneTypes.setText(bean.getAccidentpronetype());
//        binding.occupationalHazardFactors.setText(bean.getOccupationalhazardscategory());
        binding.occupationalHazardFactorsEdit.setText(bean.getOccupationalhazardsname());
        binding.mainHazardHarmfulness.setText(bean.getMaindangerharmfulness());
        binding.controlMeasures.setText(bean.getControlmeasures());
    }

    private void delDangerousPoint(String id) {
        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .DelDangerousPoint(id)
                .compose(RxUtils.<ECHttpResponse<BaseReturn>>bindToLifecycle(this))
                .compose(RxUtils.<ECHttpResponse<BaseReturn>>schedulersTransformer())
                .compose(RxUtils.<ECHttpResponse<BaseReturn>>exceptionTransformer())
                .subscribe(new ECObserver<BaseReturn>() {
                    @Override
                    protected void _onNext(BaseReturn baseReturn) {
                        dismissLoading();
                        ToastMessage("删除成功");
                        finishActivityForResult();
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }

}
